package com.tevintao.marketingsystem.task;

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;


public class ChangeIconTaskProcess extends SetTaskExtra {
    private static ChangeIconTaskProcess ourInstance = new ChangeIconTaskProcess();

    public static ChangeIconTaskProcess getInstance() {
        return ourInstance;
    }

    private ChangeIconTaskProcess() {
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        Object iconObj = getExtra(WxUiViewConfig.CHANGE_ICON_PATH);
        if (null == iconObj) {
            taskError(v.getContext(), WxTaskManger.TASK_SATE_EXTRA_ERROR, "没有图片");
            return true;
        }
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inContact = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_ME);
            if (inContact) {
                LogManager.getInstance().logInfo("当前位于我按钮所在的列表");
                View myName = v.findViewById(WxUiViewConfig.MY_NAME_ITEM);
                WxUiViewConfig.printViewGroup((ViewGroup) v);
                if (null != myName) {
                    ViewOperation.clickView(myName);
                }
            }
        } else if (WxUiViewConfig.VIEW_ME_INFO.equals(contextName)) {
            View signView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, 16908310, "头像");
            LogManager.getInstance().logInfo("signView = " + signView);
            if (null != signView) {
                ViewOperation.clickView(signView);
            }
        } else if (WxUiViewConfig.VIEW_ALBUM_PREVIEW.equals(contextName)) {
            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View gridView = v.findViewById(WxUiViewConfig.GRID_VIEW);
            if (null != gridView && gridView instanceof GridView) {
                ViewGroup gridGroup = (ViewGroup) gridView;
                if (gridGroup.getChildCount() > 1) {
                    View secondView = gridGroup.getChildAt(1);
                    if (null != secondView) {
                        ViewOperation.clickView(secondView);
                    }
                }
            }
        } else if (WxUiViewConfig.VIEW_CROP_IMAGE_NEW.equals(contextName)) {
            View useButton = v.findViewById(WxUiViewConfig.USE_NEW_ICON_BUTTON);
            if (null != useButton) {
                ViewOperation.clickView(useButton);
                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "切换图像成功");
            }
        }
        return false;
    }
}
